<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件getClientInfoById
use think\Db;
use think\Cache;
//字符串截取以及截取后输入...
function str_lenght($str,$len){
    $str=strip_tags($str);
    $str_len=mb_strlen($str,'utf-8');
    if($str_len>$len){
        $a='...';
    }else{
        $a='';
    }
    $str=mb_substr($str,0,$len,'utf-8')."$a";
    return $str;
}
//截取文章内容ueditor上传的所有图片路径
function extract_attrib($tag) {
    preg_match_all('/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i', $tag, $matches);
    $ret = array();
    foreach($matches as $k => $v) {
        $ret=$v;
    }
    return $ret;
}
//curl
function request_post($url = '',$param = '' ,$isJson = '') {

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if($param !==''){
        curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
        curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
    }
    if($isJson)
    {
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_HTTPHEADER,array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length:' . strlen($param),
                'Cache-Control: no-cache',
                'Pragma: no-cache'
        ));
    }
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}

function hideContactNum($str)
{
    $firstLen = 3;
    $lastLen = 4;
    if(mb_strlen($str,'utf-8') < 8){
        $firstLen = 2;
        $lastLen = 1;
    }

    return mb_substr($str, 0, $firstLen).'****'.mb_substr($str, $lastLen*-1);
}

function idToAdminname($id)
{
    if($id)
    {
        return db('admin')->where('id',$id)->value('name');
    }
}

function idToCateName($id)
{
    if($id)
    {
        return db('article_cate')->where('id',$id)->value('cate_name');
    }
}

function getCountByClientId($id,$type='all',$return='html')
{
    if($id)
    {
        $list = db('client')
                ->alias('c')
                ->field('c.id,c.status,c.xaid,a.name as aname')
                ->join('admin a','a.id=c.xaid')
                ->where('c.union_id',$id)
                ->where('c.status','IN','1,3,4')
                ->cache(600)
                ->select();
        $dataArr = [];
        foreach ($list as $k => $v) 
        {
            $dataArr[$v['status']]['name'][] = $v['aname'];
            $dataArr[$v['status']]['admin_id'][] = $v['xaid'];
            $dataArr[$v['status']]['count']++;
        }
        foreach ($dataArr as $k => $v) 
        {
            if($k == 1)
            {
                $dataArr[$k]['text'] = "<span title='此客户已被".implode('、', $v['name'])."{$v['count']}人录入' class='layui-font-orange '>【录入状态】</span>";
            }
            else if($k == 3)
            {
                $dataArr[$k]['text'] = "<span title='此客户已被".implode('、', $v['name'])."{$v['count']}人保A' class='layui-font-orange'>【保A状态】</span>";
            }
            else if($k == 4)
            {
                $dataArr[$k]['text'] = "<span title='此客户已被".implode('、', $v['name'])."签单' class='layui-font-red'>【签单状态】</span>";
            }
            if($v['admin_id'][0] == session('uid','','admin') && $v['count'] == 1)
            {
                unset($dataArr[$k]);
            }
        }

        if($return == 'array')
        {
            return $dataArr;
        }
        $text;
        if($type == 'all' || $type == 'qiandan' || $type == 'baoa')
        {
            $text .= $dataArr[4]['text'];
        }
        if($type == 'all' || $type == 'baoa')
        {
            $text .= $dataArr[3]['text'];
        }
        if($type == 'all')
        {
            $text .= $dataArr[1]['text'];
        }
        return $text;
        //print_r($dataArr);
        /*
        if($type == 'all' || $type == 'qiandan' || $type == 'baoa')
        {
            $clientQdCount = db('client')->where('union_id',$id)->where('status',4)->count();
            if($clientQdCount >= 1)
            {
                return  "<span title='此客户已有人签单' class='layui-font-red'>【被签单】　</span>";
            }
        }
        if($type == 'all' || $type == 'baoa')
        {
            $clientBaCount = db('client')->where('union_id',$id)->where('status',3)->count();
            if($clientBaCount >= 3)
            {
                return  "<span title='此客户已有被3人保A' class='layui-font-orange'>【被保A】　</span>";
            }
        }
        if($type == 'all')
        {
            $clientGlCount = db('client')->where('union_id',$id)->where('status','IN','1,3')->count();
            if($clientGlCount >= 5)
            {
                return  "<span title='此客户已有被5人关联' class='layui-font-orange'>【被关联】　</span>";
            }
        }*/

    }
}

function getDirContent($path){
    if(!is_dir($path)){
        return false;
    }
    $arr = array();
    $data = scandir($path);
    foreach ($data as $value){
        if($value != '.' && $value != '..'){
            $arr[] = $value;
        }
    }
    return $arr;
}


function getClientInfoById($id,$field)
{
    if($id)
    {
        return db('client')->where('id',$id)->value($field);
    }
}

function getFollowText($status)
{
    switch ($status) 
    {
        case 1:
            return '<span class="layui-font-green">通过</span>';
            break;
        case 2:
            return '<span class="layui-font-blue">未通过</span>';
            break;
        case 3:
            return '<span class="layui-font-red">不需要</span>';
            break;
        case 4:
            return '<span class="layui-font-orange">审批中</span>';
            break;
        case 5:
            return '<span class="layui-font-red">有合作平台</span>';
            break;
        
        default:
            return '暂无';
            break;
    }
}

function getCheckStatus($status)
{     
    switch ($status) 
    {
        case 0:
            return '<span class="layui-font-blue">未校对</span>';
            break;
        case 1:
            return '<span class="layui-font-green">确认</span>';
            break;
        case -1:
            return '<span class="layui-font-red">否</span>';
            break;
        case 3:
            return '<span class="layui-font-blue">已启动</span>';
            break;
        case 4:
            return '<span class="layui-font-blue">未启动</span>';
            break;
        case 5:
            return '<span class="layui-font-red">黄单</span>';
            break;
        default:
            return ' ';
            break;
    }
}

function get_client_ip($type = 0) {
    $type       =  $type ? 1 : 0;
    static $ip  =   NULL;
    if ($ip !== NULL) return $ip[$type];
    if($_SERVER['HTTP_X_REAL_IP']){//nginx 代理模式下，获取客户端真实IP
        $ip=$_SERVER['HTTP_X_REAL_IP'];     
    }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {//客户端的ip
        $ip     =   $_SERVER['HTTP_CLIENT_IP'];
    }elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {//浏览当前页面的用户计算机的网关
        $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $pos    =   array_search('unknown',$arr);
        if(false !== $pos) unset($arr[$pos]);
        $ip     =   trim($arr[0]);
    }elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip     =   $_SERVER['REMOTE_ADDR'];//浏览当前页面的用户计算机的ip地址
    }else{
        $ip=$_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $long = sprintf("%u",ip2long($ip));
    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}

function get_depart_count($department_id)
{
    return db('admin')->where('department_id',$department_id)->where('status',1)->count();
}

function get_depart_name($department_id)
{
    return db('admin')->where('department_id',$department_id)->where('group_id',5)->value('group_name');
}

function get_vote_start_at($live_id)
{
    return Db::connect("live_db_config")->table('ims_wzbagent_vote_setting')->where('live_id',$live_id)->value('start_at');
}

